package com.lww.feature.test.array;

import org.junit.Test;

/**
 * System.arraycopy()只拷贝了对象的引用，基本类型是拷贝数据本身
 * author: liweiwei
 * Date: 2018/12/28
 */
public class Copy {

    public Copy() {
        System.out.println("+++++++++++++++++");
    }
    /**
     * System.arraycopy()只拷贝了对象的引用
     */
    @Test
    public void test() {
        BeanBeCopy[] s = new BeanBeCopy[5];
        BeanBeCopy[] o = new BeanBeCopy[5];

        for (int i=0;i<5;i++) {
            s[i]=new BeanBeCopy(i+"");
        }
        System.arraycopy(s,0,o,0,5);
        s[0].setFiled(9+"");
        System.out.println(s[0].getFiled());
        System.out.println(o[0].getFiled());
    }


    /**
     * System.arraycopy()基本类型是拷贝数据本身
     */
    @Test
    public void testBaseType() {
        int[] s = new int[5];
        int[] o = new int[5];

        for (int i=0;i<5;i++) {
            s[i]=i;
        }
        System.arraycopy(s,0,o,0,5);
        s[0]=9;
        System.out.println(s[0]);
        System.out.println(o[0]);
    }


    public static class BeanBeCopy{
        private String filed;

        public BeanBeCopy(String filed) {
            this.filed = filed;
        }

        public String getFiled() {
            return filed;
        }

        public void setFiled(String filed) {
            this.filed = filed;
        }
    }
}
